package org.osami.gateway.test.demo;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.codec.binary.Base64;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class ScenarioActivatorImpl implements BundleActivator {

	public static final String BASE_URL = "http://localhost:9000";

	public static final String USERNAME = "osami";

	public static final String PASSWORD = "osami";

	public void start(BundleContext context) throws Exception {
		// Creates identities with linkage
		doGet("/identity/create");
		doGet("/identity/create?parent-id=0");
		doGet("/identity/create?parent-id=0");

		doGet("/identity/create?parent-id=1");
		doGet("/identity/create?parent-id=1");
		doGet("/identity/create?parent-id=1");

		doGet("/identity/create?parent-id=2");
		doGet("/identity/create?parent-id=2");

		doGet("/identity/create?parent-id=1");
		doGet("/identity/create?parent-id=1");
		doGet("/identity/create?parent-id=1");

		// Set friendly name
		doGet("/identity/0/change/identity.friendly.name?new-value=Home");
		doGet("/identity/1/change/identity.friendly.name?new-value=Lounge");
		doGet("/identity/2/change/identity.friendly.name?new-value=Bedroom");

		doGet("/identity/3/change/identity.friendly.name?new-value=Outside+Sensor");
		doGet("/identity/4/change/identity.friendly.name?new-value=Couch+Sensor");
		doGet("/identity/5/change/identity.friendly.name?new-value=TV+Sensor");

		doGet("/identity/6/change/identity.friendly.name?new-value=Outside+Sensor");
		doGet("/identity/7/change/identity.friendly.name?new-value=Bed+Sensor");

		doGet("/identity/8/change/identity.friendly.name?new-value=TV+Set+Plug");
		doGet("/identity/9/change/identity.friendly.name?new-value=Light1+Plug");
		doGet("/identity/10/change/identity.friendly.name?new-value=Computer+Plug");

		// Additional Properties
		doGet("/identity/0/add?property-name=family.name&property-value=Doe");
		doGet("/identity/1/add?property-name=room.name&property-value=Lounge");
		doGet("/identity/2/add?property-name=room.name&property-value=Bedroom");

		// Bind !Jennic
		doGet("/identity/3/bind?bound-identifier=FD04:0BD3:80E8:0001:0215:8D00:000E:B15D");
		doGet("/identity/4/bind?bound-identifier=FD04:0BD3:80E8:0001:0215:8D00:000B:1D51");
		doGet("/identity/5/bind?bound-identifier=FD04:0BD3:80E8:0001:0215:8D00:0008:61F2");
		doGet("/identity/6/bind?bound-identifier=FD04:0BD3:80E8:0001:0215:8D00:000F:BEF7");
		doGet("/identity/7/bind?bound-identifier=FD04:0BD3:80E8:0001:0215:8D00:0007:DDCA");

		// Bind !SmartPlugs
		doGet("/identity/8/bind?bound-identifier=0A32010E");
		doGet("/identity/9/bind?bound-identifier=0A320147");
		doGet("/identity/10/bind?bound-identifier=0A32014A");

		// End !

	}

	public void stop(BundleContext context) throws Exception {
		// TODO Auto-generated method stub

	}

	private static void doGet(String relativeUrl) throws IOException {
		URL url = new URL(BASE_URL + relativeUrl);
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		String encoded = new String(
				Base64.encodeBase64((USERNAME + ":" + PASSWORD).getBytes()));
		connection.setRequestProperty("Authorization", "Basic " + encoded);
		connection.connect();
		System.err
				.println("org.osami.gateway.test.demo.ScenarioActivatorImpl> "
						+ connection.getResponseCode() + " " + relativeUrl);
		connection.disconnect();
	}

}
